home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / SeqPups / appsrc / autoseq.src / CSequence.H < prev    next >
Text File  |  1996-07-05  |  4KB  |  162 lines

  1. //    ============================================================================
  2. //    CSequence.H                                                        80 columns
  3. //    Reece Hart    (reece@ibc.wustl.edu)                                tab=4 spaces
  4. //    Washington University School of Medicine, St. Louis, Missouri
  5. //    This source is hereby released to the public domain.  Bug reports, code
  6. //    contributions, and suggestions are appreciated (to email address above).
  7. //    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -
  8. //    CSequence is a template which uses a doubly-linked list to represent a lists
  9. //    of any type of class.  This class grew from the need for a simple list
  10. //    structure, and as such, is not a particularly robust implementation.  It
  11. //    currently provides no iterator functions, serial access is used for indexed
  12. //    references (ie. linear search).
  13. //    Note that this class stores the list object type explicitly, rather than a
  14. //    pointer to the object.  This has advantages and disadvantages, but suffice
  15. //    it to say that the user assumes responsibility for delete'ing any space s/he
  16. //    allocates and references in the list object, although the list object
  17. //    itself will be deleted correctly.
  18. //    Users may wish to override the assingment and comparison operators for
  19. //    specialized types.
  20. //
  21. //    SLATED IMPROVEMENTS
  22. //    *    For now, there is no error handling other than returning TRUE in the
  23. //        case of an error.  I will extend to exceptions or error signals when the
  24. //        code becomes more stable (ie. when I'm finished designing).
  25. //    *    add support (via fx pointer in list header) to a binary rel'n operator
  26. //    *    filter method
  27. //
  28. //    MODIFICATION HISTORY
  29. //    93.07.31    Reece    Original coding
  30. //    93.11.11    Reece    First release
  31. //    ========================================|===================================
  32.  
  33. #ifndef _H_CSequence                        // include this file only once
  34. #define _H_CSequence
  35.  
  36. #include    <iostream.h>
  37. #include    <fstream.h>
  38. #include    "RInclude.H"
  39.  
  40. template<class T>
  41. struct SeqNode                                // sequence node
  42.     {
  43.     SeqNode<T>*    next;
  44.     SeqNode<T>*    prev;
  45.     T            data;
  46.  
  47.                 SeqNode(void):                // constructor; no arguments
  48.                     next(NULL),
  49.                     prev(NULL)
  50.                     {}
  51.  
  52.                 SeqNode(                    // constructor with args:
  53.                     T d,                    //   data
  54.                     SeqNode<T>* p=NULL,        //   predecessor
  55.                     SeqNode<T>* n=NULL):    //   successor
  56.                     data(d),
  57.                     next(n),
  58.                     prev(p)
  59.                     {
  60.                     if (NULL != n) n->prev = this;            // insert the node
  61.                     if (NULL != p) p->next = this;
  62.                     }
  63.  
  64.                 ~SeqNode(void)                // destructor
  65.                     {
  66.                     if (NULL != next) next->prev = prev;    // excise the node
  67.                     if (NULL != prev) prev->next = next;
  68.                     }
  69.     };
  70.  
  71. template<class T>
  72. class    CSequence
  73.     {
  74.     private:
  75.     static vrsn    version;
  76.  
  77.     protected:
  78.     ulong        size;
  79.     SeqNode<T>*    first;
  80.     SeqNode<T>*    last;
  81.  
  82.     public:
  83.     vrsn&        Version()
  84.                     { return version; }
  85.  
  86.                 CSequence(void):            // constructor
  87.                     first(0),
  88.                     last(0),
  89.                     size(0)
  90.                     {}
  91.  
  92.                 ~CSequence(void)            // destructor
  93.                     {
  94.                     for(SeqNode<T>* sn=first;NULL != sn;sn=sn->next)
  95.                         delete sn;
  96.                     }
  97.  
  98.     ulong        Size(void)                    // return # elements in list
  99.                     {
  100.                     return size;
  101.                     }
  102.  
  103.     bool        Prepend(T item);
  104.     bool        Append(T item);
  105.     bool        Insert(                        // insert item @ before
  106.                     T item,
  107.                     ulong before)
  108.                     {
  109.                     return InsertNode(item,(*this)%(before));
  110.                     }
  111.  
  112.     bool        Remove(ulong index)            // excise item @ index
  113.                     {
  114.                     return RemoveNode((*this)%(index));
  115.                     }
  116.  
  117.     ulong        operator()(T item)            // returns index of 1st occurrence
  118.                     {
  119.                     SeqNode<T>*    sn = first;    
  120.                     ulong        index = 1;
  121.                     while (NULL != sn)
  122.                     if (sn->data == item)
  123.                         return index;
  124.                     else
  125.                         { sn = sn->next; index++; }
  126.                     return 0;
  127.                     }    
  128.  
  129.     T&            operator[](ulong index)        // returns item at 0<index<=Size()-1
  130.                     {
  131.                     SeqNode<T>* sn = (*this)%(index);
  132.                     return sn->data;
  133.                     }
  134.  
  135.     friend
  136.     ostream&    operator<<(                    // put endl delimt'd list on stream
  137.                     ostream&    os,
  138.                     CSequence<T>& seq)
  139.                     {
  140.                     for(SeqNode<T>* sn=seq.first;NULL!=sn;sn=sn->next)
  141.                         os << sn->data << endl;
  142.                     return os;
  143.                     }
  144.  
  145.     private:
  146.     SeqNode<T>* operator%(ulong index)        // ret'n SeqNode* @ 0<=index<=size-1
  147.                     {
  148.                     SeqNode<T>*    sn = first;
  149.                     if (index > size-1)        // index must be >=0 (ulong)
  150.                         return NULL;
  151.                     while (index != 0)
  152.                         { sn = sn->next; index--; }
  153.                     return sn;
  154.                     }
  155.  
  156.     bool    InsertNode(T item,SeqNode<T>* before);
  157.     bool    RemoveNode(SeqNode<T>* node);
  158.     };
  159.  
  160. #endif                                        // conditional inclusion
  161.  
  162.